/***************************************************************************\
 * Copyright (C) by Keio University
 * BilinearInterpolation.cu created in 04 2012.
 * Mail : fdesorbi@hvrl.ics.keio.ac.jp
 *
 * BilinearInterpolation.cu is part of the HVRL Engine Library.
 *
 * The HVRL Engine Library is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * The HVRL Engine Library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
\***************************************************************************/

#ifndef BILINEAR_INTERPOLATION__CU__
#define BILINEAR_INTERPOLATION__CU__

#ifdef WIN32
#define NOMINMAX
#include <windows.h>
#endif

#include <hvrl/gpgpu/cuda/Prototypes.hpp>
#include <hvrl/gpgpu/cuda/Tools.cuh>


inline __device__ float4 compute_weight(const float2& coordinate){
	float2 frac = coordinate - floorf(coordinate);
	return make_float4(
			(1.0f - frac.x) * (1.0f - frac.y),
			frac.x * (1.0f - frac.y),
			frac.x * frac.y,
			(1.0f - frac.x) * frac.y);
}


/**
 * \brief The Kernel computes the bilinear interpolation
 *
 * based on: http://en.wikipedia.org/wiki/Bilinear_interpolation
 *
 */
__forceinline__ __device__ float3 bilinear_interpolation_rgb(
		const float2& coordinate,
		const float3& point1,
		const float3& point2,
		const float3& point3,
		const float3& point4){

		float4 coeff = compute_weight(coordinate);

		return make_float3(
		point1.x * coeff.x + point2.x * coeff.y + point3.x * coeff.z + point4.x * coeff.w,
		point1.y * coeff.x + point2.y * coeff.y + point3.y * coeff.z + point4.y * coeff.w,
		point1.z * coeff.x + point2.z * coeff.y + point3.z * coeff.z + point4.z * coeff.w);

}

__forceinline__ __device__ uchar3 bilinear_interpolation_rgb(
		const float2& coordinate,
		const uchar3& point1,
		const uchar3& point2,
		const uchar3& point3,
		const uchar3& point4){

		float4 coeff = compute_weight(coordinate);

		return make_uchar3(
		point1.x * coeff.x + point2.x * coeff.y + point3.x * coeff.z + point4.x * coeff.w,
		point1.y * coeff.x + point2.y * coeff.y + point3.y * coeff.z + point4.y * coeff.w,
		point1.z * coeff.x + point2.z * coeff.y + point3.z * coeff.z + point4.z * coeff.w);

}



#endif

